﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.Control;
using TelnetRouterApplication.Utils;

namespace TelnetRouterApplication.View
{
    public partial class SendMessageForm : Form
    {
        private static bool _DEBUG;
        public static bool DEBUG
        {
            get { return SendMessageForm._DEBUG; }
            set { SendMessageForm._DEBUG = value; }
        }

        public SendMessageForm()
        {
            DEBUG = MainForm.DEBUG;
            InitializeComponent();
            msgBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.msgBox_KeyDown);
        }

        private void SendBtn_Click(object sender, EventArgs e)
        {
            string msg = msgBox.Text;
            ConnectionHandler.GetInstance().SendMessage(msg);
            List<string> messages = ConnectionHandler.GetInstance().ReceiveMessage();
            RefreshLog(messages);
        }

        private void ClenBtn_Click(object sender, EventArgs e)
        {
            msgBox.Text = "";
            logAreaTxt.Text = "";
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void RefreshLog(List<string> messages)
        {
            foreach (string message in messages)
            {
                logAreaTxt.Text = "\r" + message + logAreaTxt.Text;
            }
        }

        private void msgBox_KeyDown(object sender, KeyEventArgs e)
        {
            //
            // Detect the KeyEventArg's key enumerated constant.
            //
            if (e.KeyCode == Keys.Enter)
            {
                SendBtn_Click(sender, null);
            }
            
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            string path = IOUtils.SaveFileDialogShow(this, "Text Files|*.txt");
            IOUtils.CreateTxtFile(path, logAreaTxt.Text);
            MessageBox.Show("Log exported!");
        }

        #region retirar no futuro
        private void SendMessageForm_Load(object sender, EventArgs e)
        {

        }

        private void msgBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        #endregion
    }
}
